﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Musicman.Common.Entities;
using Musicman.Business;
using System.Net.Mail;
using Musicman.Utilities;
using System.Text.RegularExpressions;

namespace MusicmanWeb.ADMIN
{
    public partial class Mail : MusicmanPage
    {
        override protected void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            if (!IsPostBack)
            {
                List<string> emails = GetEmailAddresses();
                emailsrepeater.DataSource = emails;
                emailsrepeater.DataBind();
            }
        }

        protected void sedingRadio_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cache.Remove("emailaddresses");
            List<string> emails = GetEmailAddresses();
            emailsrepeater.DataSource = emails;
            emailsrepeater.DataBind();
        }

        protected void AddAdress_Command(object sender, CommandEventArgs e)
        {
            bool valid = true;
            List<string> emails = GetEmailAddresses();
            string email = emailtxt.Text.Trim();
            valid = !String.IsNullOrEmpty(email) && Regex.IsMatch(email, Musicman.Common.Constants.EmailRegex);
            erroLbl.Visible = !valid;
            if (valid && !emails.Contains(email))
            {
                emails.Add(email);
            }
            if (valid)
            {
                emailtxt.Text = "";
            }
            emailsrepeater.DataSource = emails;
            emailsrepeater.DataBind();
        }

        protected void Emailsrepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                ((System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("emailLbl")).InnerText = e.Item.DataItem.ToString();
                ((LinkButton)e.Item.FindControl("remove")).CommandArgument = e.Item.DataItem.ToString();
            }
        }

        protected void Emailsrepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            List<string> emails = GetEmailAddresses();
            emails.Remove(e.CommandArgument.ToString());
            emailsrepeater.DataSource = emails;
            emailsrepeater.DataBind();
        }

        public List<string> GetEmailAddresses()
        {
            if (Cache["emailaddresses"] == null)
            {
                List<string> emals = new List<string>();
                List<User> recipiants = null;
                if (sedingRadio.SelectedValue == "subscribers")
                {
                    recipiants = UserManager.Instance.LoadMailSubscribers();
                }
                else
                {
                    recipiants = UserManager.Instance.LoadAllUsers();
                }

                for (int i = 0; i < recipiants.Count; i++)
                {
                    emals.Add(recipiants[i].Emailaddress);   
                }
                Cache.Insert("emailaddresses", emals, null,
                                System.Web.Caching.Cache.NoAbsoluteExpiration,
                                new TimeSpan(0, 3, 0));
            }
            return Cache["emailaddresses"] as List<string>;
        }

        protected void Update_Button(object sender, CommandEventArgs e)
        {
            string message = this.mailcontent.Value;
            List<string> emails = GetEmailAddresses();
            if (emails != null && emails.Count != 0)
            {
                MailMessage email = new MailMessage();
                email.From = new MailAddress("musicmanservices@musicman.com", "Musicman");
                for (int i = 0; i < emails.Count; i++)
                {
                    MailAddress ea = new MailAddress(emails[i]);

                    if (!email.To.Contains(ea))
                    {
                        email.To.Add(ea);
                    }
                }
                email.Subject = subjecttxt.Text;
                email.Body = message;
                email.IsBodyHtml = true;
                MailSender.Instance.SendMailAsync(email);
                this.lblstatus.Text = "Mail sucessfully sent";
                this.mailcontent.Value = "";
            }
        }
    }
}
